package examples.mail;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;


public class Db {
	private static DataSource dataSource=null;

	public static DataSource getDataSource() throws Exception{
		if(dataSource==null){
			Properties p = new Properties();
	        p.setProperty("driverClassName", "com.mysql.jdbc.Driver");
	        p.setProperty("url", "jdbc:mysql://127.0.0.1:3306/nml?useUnicode=true&characterEncoding=utf-8");
	        p.setProperty("password", "");
	        p.setProperty("username", "root");
	        p.setProperty("maxActive", "30");
	        p.setProperty("maxIdle", "10");
	        p.setProperty("maxWait", "1000");
	        p.setProperty("removeAbandoned", "false");
	        p.setProperty("removeAbandonedTimeout", "120");
	        p.setProperty("testOnBorrow", "true");
	        p.setProperty("logAbandoned", "true");
	        p.setProperty("defaultAutoCommit", "false");
	        
	        
	     
			dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
		}
		return dataSource;
	}
	
	public static QueryRunner getQueryRunner() {
		try {
			return new QueryRunner(  Db.getDataSource()   );
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	
	

}
